import matplotlib.pyplot as plt
import numpy as np


def plot_heart():
    data = []
    with open('./output/E_output.txt', 'r') as file:
        for line in file.readlines():
            line = line.strip('\n')
            line = line.split()
            data.append(line)

    # read in points
    dim1 = []
    dim2 = []
    for i in range(0, int(len(data)/4)):
        if i <= len(data):
            j = i*4
            num = int(data[j+1][3])
            dim1.append(data[j+2])
            dim2.append(data[j+3])

            x = []
            y = []
            x1 = np.linspace(-np.sqrt(3), np.sqrt(3), 1000)
            y1 = fun_heart1(x1)
            y2 = fun_heart2(x1)
            for k in range(0, len(dim1[i])):
                x.append(float(dim1[i][k]))
                y.append(float(dim2[i][k]))

            plt.plot(x, y, label='curve_fitting')
            plt.plot(x1, y1, label='true_value_up')
            plt.plot(x1, y2, label='true_value_down')
            plt.title('heart function')
            plt.legend()
            plt.savefig("./pic/E_"+str(num)+".png")
            plt.show()


def fun_heart1(x):
    return 2.0 / 3.0 * (np.sqrt(np.abs(x)) + np.sqrt(3 - x ** 2))


def fun_heart2(x):
    return 2.0 / 3.0 * (np.sqrt(np.abs(x)) - np.sqrt(3 - x ** 2))


plot_heart()
